package demo;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.LabelUI;
public class WallPaperDemo extends JFrame{

	public WallPaperDemo(){
		init();
	}
	
	private void init(){
		setTitle("Wall Paper Demo");
		setSize(600,350);
		setContentPane(createContentpane());
		pack();
		setLocationRelativeTo(null);
		//Component labelUI = new WallPaperLayerUI();
		//add(labelUI);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}

	private Container createContentpane() {
		JPanel panel = new JPanel();
		JLabel label = new JLabel("Name:");
		final JTextField name = new JTextField(20);
		JButton btn = new JButton("Reverse");
		panel.add(label);
		panel.add(name);
		panel.add(btn);
		
		btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String text = name.getText();
				text = new StringBuffer(text).reverse().toString();
				name.setText(text);
			}
		});
		return panel;
	}
}

class WallPaperLayerUI extends  LabelUI{

	@Override
	public void paint(Graphics g, JComponent c) {
		super.paint(g, c);
		Graphics2D g2 = (Graphics2D) g.create();
		int width = c.getWidth();
		int height = c.getHeight();
		g2.setComposite(AlphaComposite.
				getInstance(AlphaComposite.SRC_OVER,.5f));
		g2.setPaint(new GradientPaint(0,0,Color.yellow,
				0,height,Color.blue));
		g2.fillRect(0, 0, width, height);
		g2.dispose();
	}
	
}
